package com.yz.platform.modules.biz.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.yz.platform.common.utils.Constant;
import com.yz.platform.common.utils.PageUtils;
import com.yz.platform.common.utils.Query;
import com.yz.platform.common.utils.R;
import com.yz.platform.modules.biz.entity.AppEntity;
import com.yz.platform.modules.biz.entity.ScriptEntity;
import com.yz.platform.modules.biz.service.AppService;
import com.yz.platform.modules.biz.service.ScriptService;
import com.yz.platform.modules.sys.controller.AbstractController;
import com.yz.platform.modules.sys.entity.SysConfigEntity;


/**
 * @author lin
 * @email
 * @date 2017-10-15 16:00:56
 */
@RestController
@RequestMapping("script")
public class ScriptController extends AbstractController {


    @Autowired
    private ScriptService scriptService;


    @Autowired
    private AppService appService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("script:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ScriptEntity> scriptList = scriptService.queryList(query);
        int total = scriptService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(scriptList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{scriptId}")
    @RequiresPermissions("script:info")
    public R info(@PathVariable("scriptId") Integer scriptId) {
        ScriptEntity script = scriptService.queryObject(scriptId);

        return R.ok().put("script", script);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("script:save")
    public R save(@RequestBody ScriptEntity script) {
        script.setCreateUser(getUserId().intValue());
        scriptService.save(script);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("script:update")
    public R update(@RequestBody ScriptEntity script) {
        script.setUpdateUser(getUserId().intValue());
        scriptService.update(script);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("script:delete")
    public R delete(@RequestBody Integer[] scriptIds) {
        scriptService.deleteBatch(scriptIds);

        return R.ok();
    }


    /**
     * App列表
     */
    @RequestMapping("/selectApp")
    @RequiresPermissions("script:save")
    public R selectApp(){

        Map<String, Object> map = new HashMap<>();
        List<AppEntity> list = appService.queryList(map);

        return R.ok().put("list", list);
    }


}
